// pages/WiFiList/WiFiList.js
const app = getApp();
const errCode = app.globalData.errCode;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    state:0,//0:正在获取WiFi信息，1:获取成功，2:获取失败，请重试，3:当前没有连接WiFi
    curWifi:'',
    wifiLsit: [
      // {
      //   SSID: "404 NOT FOUND",
      //   BSSID: "2c:b2:1a:5c:c9:bd",
      //   secure: true,
      //   signalStrength: 99,
      //   frequency: 2412,
      // },
      // {
      //   SSID: "404 NOT FOUND",
      //   BSSID: "2c:b2:1a:5c:c9:bd",
      //   secure: true,
      //   signalStrength: 60,
      //   frequency: 2412,
      // },
      // {
      //   SSID: "404 NOT FOUND",
      //   BSSID: "2c:b2:1a:5c:c9:bd",
      //   secure: true,
      //   signalStrength: 40,
      //   frequency: 2412,
      // },
      // {
      //   SSID: "404 NOT FOUND",
      //   BSSID: "2c:b2:1a:5c:c9:bd",
      //   secure: true,
      //   signalStrength: 20,
      //   frequency: 2412,
      // }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAllWifiinfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getAllWifiinfo() {//获取所有wifi信息
    
    wx.startWifi({
      success: (res) => {
        this.getWifiList();
        this.getConnectedWifi();
      },
      fail: (res) => {
        console.log(res);
        app.toast('WiFi初始化失败', res);
        this.setData({
          state:2
        })
      }
    })
  },

  getWifiList() {//获取wifi列表
    this.setData({
      state:0
    })
    wx.getWifiList({
      success: (res) => {
        wx.onGetWifiList(res => {
          console.log(res.wifiList)
          this.setData({
            wifiLsit: res.wifiList,
            state: 1
          })
        })
      },
      fail: (res) => {
        console.log(res);
        if(res.errMsg == "getWifiList:fail auth deny"){//没有授权
          this.getAuth();
        }else{
          app.toast(errCode[res.errCode], res)
        }
        this.setData({
          state:2
        })
      },
    })
  },

  getConnectedWifi() {//获取已连接wifi的信息
    wx.getConnectedWifi({
      success: (res) => {
        console.log(res);
        this.setData({
              curWifi: res.wifi
            })
      },
      fail: (res) => {
        console.log(res);
        if(res.errMsg == "getConnectedWifi:fail:currentWifi is null"){
          this.setData({
            state:3
          })
        }else{
          app.toast(errCode[res.errCode], res)
        }
      },
    })
  },
  createWifiCode(e){//创建Wi-Fi码
    let wifiName = '';
    if(e.currentTarget.dataset.flag == "cur"){
      wifiName = this.data.curWifi.SSID;
    }else{
      wifiName = e.currentTarget.dataset.wifiname;
    }
    console.log(wifiName);
    if(wifiName){
      wx.navigateTo({
        url: '../createWiFi/createWiFi?wifiName=' + wifiName,
      })
    }else{
      wx.showToast({
        title: '无法读取到WiFi名称',
        icon:"none"
      })
    }
    
  },
  getAuth(){
    wx.getSetting({
      success: res => {
        if (res.authSetting["scope.userLocation"]) {//查看位置权限
          this.getAllWifiinfo();
        } else {
          wx.showModal({
            title: '提示',
            content: '需要位置权限\n获取WiFi信息',
            confirmText: '去授权',
            success: res => {
              if (res.confirm) {
                wx.openSetting({
                  success: res => {
                    console.log('授权结果', res);
                    if (res.authSetting["scope.userLocation"]) {
                      this.getAllWifiinfo();
                    }
                  }
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
                this.setData({
                  state:2
                })
              }
            }
          })
        }
      }
    })
  }
})